import { axios } from "@/services/axios";
import {useEffect, useMemo, useState} from "react";

function  getPortalNameByCode(portal, portal_code)
{
  for (var i in portal) {
    if (portal[i].code === portal_code) return portal[i].name;
  }
}

function  removeItemByKey(items, key) {
  for ( var i = 0; i < items.length; i++) {
    let item = items[i]
    if (item.key === key) {
      items.splice(i, 1);
      break;
    }
    if (item.hasOwnProperty("children")) {
      const r = removeItemByKey(item.children, key);
      if (r.length !== item.children.length) {
        item.children = r;
        break;
      }
    }
  }
  return items;
}

function existItemByKey(items, key) {
  var is_tree = false;
  for (var item in items) {
    if (items[item].key === key) {
      is_tree = true;
      break;
    }
    if (items[item].hasOwnProperty("children")) {
      const r = existItemByKey(items[item].children, key);
      if (r) {
        is_tree = true;
        break;
      }
    }
  }
  return is_tree;
}

function existItemById(items, id) {
  var is_tree = false;
  for (var item in items) {
    if (items[item].id === id || items[item].dashboard === id) {
      is_tree = true;
      break;
    }
    if (items[item].hasOwnProperty("children")) {
      const r = existItemById(items[item].children, id);
      if (r) {
        is_tree = true;
        break;
      }
    }
  }
  return is_tree;
}

function filterItemsByDashboard(items) {
  var res = []
  for (var item in items) {
    if (items[item].hasOwnProperty("dashboard")) {
      if (!!items[item].dashboard && items[item].dashboard !== "URL" && items[item].dashboard !== "POP") {
        items[item]["id"] = items[item].dashboard;
        items[item]["name"] = items[item].dashboardTitle || items[item].label;
        res.push(items[item]);
      }       
    } else if (items[item].hasOwnProperty("id") && parseInt(items[item]["id"]) > 0 ) {
      items[item]["parameter"] = null;
      items[item]["refreshRate"] = 0;
      res.push(items[item]);
    }
    if (items[item].hasOwnProperty("children")) {
      const r = filterItemsByDashboard(items[item].children);
      if (r) res = res.concat(r);
    }
  }
  return res;
}

function filterItemsByOpenkeys(items) {
  var res = []
  for (var item in items) {
    if (items[item].hasOwnProperty("dashboard")) {
      if (items[item].hasOwnProperty("children")) {
        if (items[item].hasOwnProperty("isopen") && items[item].isopen) {
          res.push(items[item].key);
        }
                
        const r = filterItemsByOpenkeys(items[item].children);
        if (r) res = res.concat(r);
      }
    }
  }
  return res;
}

function itemHasChildren(item) {
  return item.hasOwnProperty("children") && item["children"].length>0;
}

function menuHasChildren(item) {
  if (item.hasOwnProperty("children") && item["children"].length>0) {
    for(var i in item.children) {
      if (item.children[i].dashboard !== "" || menuHasChildren(item.children[i])) return true;
    }
  }
  return false;
}

const Portal = {
  getBuiltIn: () => axios.get(`api/portal/builtin`),
  getShortcut: code => axios.get(`api/portal/shortcut/${code}`),
  get: code => axios.get(`api/portal/${code}`),
  delete: code => axios.delete(`api/portal/${code}`),
  save: (code,data) => axios.post(`api/portal/${code}`, data),
  saveItems: (code,data) => axios.post(`api/portal/items/${code}`, data),
  items: code => axios.get(`api/portal/items/${code}`),
  all: () => axios.get(`api/portal/all`),
  create: data => axios.post(`api/portal/all`, data),
  getPortalNameByCode,
  itemHasChildren,
  menuHasChildren,
  removeItemByKey,
  existItemByKey,
  existItemById,
  filterItemsByDashboard,
  filterItemsByOpenkeys,
};

export function useQueryPortalItems(portalcode) {
  const [optionsLoaded, setOptionsLoaded] = useState(false);
  const [options, setOptions] = useState([]);

  useEffect(() => {
    let cancelDataSourceLoading = false;
    Portal.items(portalcode).then(data => {
      if (!cancelDataSourceLoading) {
        setOptionsLoaded(true);
        setOptions(data);
    }});

    return () => {
      cancelDataSourceLoading = true;
    };
  }, [portalcode]);

  return useMemo(() => ({ optionsLoaded, options }), [optionsLoaded, options]);
}

export default Portal;
